package com.tongyi.lizhishop.activitys;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.UserApi;
import com.tongyi.lizhishop.beans.IndexBean;
import com.tongyi.lizhishop.cache.RxCacheManager;
import com.tongyi.lizhishop.common.BaseActivity;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.tongyi.lizhishop.utils.QQUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;
import com.zchu.rxcache.RxCache;
import com.zchu.rxcache.data.CacheResult;
import com.zchu.rxcache.stategy.CacheStrategy;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity implements RationaleListener {


    private static final int NEED_RELOAD = 10;
    @BindView(R.id.alertMsg)
    View alertMsg;
    @BindView(R.id.shadowImageHeader)
    CircleImageView shadowImageHeader;
    @BindView(R.id.userName)
    TextView userName;
    @BindView(R.id.userTele)
    TextView userTele;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.moneyCode)
    TextView moneyCode;
    @BindView(R.id.evaluation)
    TextView evaluation;
    @BindView(R.id.todaymoney)
    TextView todaymoney;
    @BindView(R.id.yesterdaymoney)
    TextView yesterdaymoney;
    @BindView(R.id.mainDetail)
    LinearLayout mainDetail;
    @BindView(R.id.managerStore)
    LinearLayout managerStore;
    @BindView(R.id.RedEnvelopeManager)
    LinearLayout RedEnvelopeManager;
    @BindView(R.id.mostor)
    LinearLayout mostor;
    @BindView(R.id.inviteFriends)
    LinearLayout inviteFriends;
    @BindView(R.id.myRecommend)
    LinearLayout myRecommend;
    @BindView(R.id.setting)
    LinearLayout setting;
    @BindView(R.id.future)
    LinearLayout future;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.awardmoney)
    TextView awardmoney;
    @BindView(R.id.redPackRemainder)
    TextView redPackRemainder;
    @BindView(R.id.storeManager)
    SuperTextView storeManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String userid = SPUtils.getInstance().getString("userid");
        LogUtils.w("userid:" + userid);
        if (TextUtils.isEmpty(userid)) {
            ActivityUtils.startActivity(this, LoginActivity.class);
            finish();
            return;
        }
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSwipeBackEnable(false);
        if (!NetworkUtils.isConnected()) {
            ToastUtils.showLong("网络不可用! 请检查网络");
        }
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.envaluationar_animation);
        evaluation.setAnimation(animation);
        animation.start();
        loadData();

        requestPerssion();
    }

    private void requestPerssion() {
        AndPermission.with(this)
                .requestCode(100)
                .permission(Permission.STORAGE)
                .rationale(this)
                .callback(this)
                .start();
    }

    // 成功回调的方法，用注解即可，这里的300就是请求时的requestCode。
    @PermissionYes(100)
    private void getPermissionYes(List<String> grantedPermissions) {
    }

    @PermissionNo(100)
    private void getPermissionNo(List<String> deniedPermissions) {
        ToastUtils.showShort("程序将无法选择图片和本地数据缓存");
    }

    @Override
    public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
   /*     AlertDialog.newBuilder(this)
                .setTitle("提示")
                .setMessage("程序将无法选择图片和本地数据缓存").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                rationale.resume();
                dialog.dismiss();
            }
        });
*/
    }

    private void loadData() {
        APIServiceManager.getInstance().create(UserApi.class)
                .getIndexData(SPUtils.getInstance().getString("userid"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<IndexBean>() {
                    @Override
                    public void onSuccess(IndexBean value) {
                        IndexBean data = value;
                        if ("succ".equals(data.getRe())) {
                            bindView(data);
                        } else {
                            onFailure(null);

                        }

                    }

                    @Override
                    public void onFailure(Throwable e) {
                        ToastUtils.showShort("请求网络失败,请重试");
                    }
                });
    }

    private void bindView(IndexBean data) {
        userName.setText(data.getArray().getSel_name());
        userTele.setText(data.getArray().getSel_iphone());
        String avatar = APIServiceManager.server_address + data.getArray().getSel_image();
        LogUtils.e("image:" + avatar);
        Glide.with(this).load(avatar).error(R.mipmap.sunset).animate(R.anim.image_scale).into(shadowImageHeader);
        todaymoney.setText(data.getArray().getCount1() + "");
        yesterdaymoney.setText(data.getArray().getCount2() + "");
        awardmoney.setText(data.getArray().getCount3() + "");
        redPackRemainder.setText(data.getArray().getCount4() + "");
        if (data.getArray().getIfS() == 1) {
            storeManager.setRightString("已完善");
        }
    }


    public void gotoLogin(View view) {
        ActivityUtils.startActivity(this, LoginActivity.class);

    }

    public void gotoSetting() {
        ActivityUtils.startActivity(this, SettingActivity.class);

    }


    @OnClick({R.id.alertMsg, R.id.mostor, R.id.storeManager, R.id.future, R.id.moneyCode, R.id.shadowImageHeader, R.id.RedEnvelopeManager, R.id.mainDetail, R.id.managerStore, R.id.evaluation, R.id.myRecommend, R.id.inviteFriends, R.id.setting, R.id.activity_main})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shadowImageHeader:
                break;
            case R.id.mostor:
/*
                ComponentName componet = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.SplashActivity");
                //pkg 就是第三方应用的包名
                //cls 就是第三方应用的进入的第一个Activity
                Intent intent11 = new Intent();
                intent11.setComponent(componet);
                intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent11);*/
                QQUtils.startQQ(this);
                break;
            case R.id.RedEnvelopeManager:
                ActivityUtils.startActivity(this, RedEnvelopeManager.class);
                break;
            /*case R.id.myWallet:
                ActivityUtils.startActivity(this, MyWalletActivity.class);
                break;*/
            case R.id.mainDetail:
                ActivityUtils.startActivity(this, ConsumeDetailActivity.class);
                break;
            case R.id.managerStore:
            case R.id.storeManager:
//                ActivityUtils.startActivity(this, StoreManagerActivity.class);
                Intent intent = new Intent(this, StoreManagerActivity.class);
                startActivityForResult(intent, NEED_RELOAD);
                break;
            case R.id.evaluation:
                ActivityUtils.startActivity(this, EvaluationActivity.class);
                break;
            case R.id.moneyCode:
                ActivityUtils.startActivity(this, CollectionMoneyActivity.class);
                break;
            case R.id.myRecommend:
                ActivityUtils.startActivity(this, MyRecommendActivity.class);
                break;
            case R.id.alertMsg:
                ActivityUtils.startActivity(this, AlertMessageActivity.class);

                break;
            case R.id.inviteFriends:
                ActivityUtils.startActivity(this, InviteFriendsActivity.class);

                break;
            case R.id.setting:
                ActivityUtils.startActivity(this, SettingActivity.class);
                break;
            case R.id.activity_main:
                break;
            case R.id.future:
                ActivityUtils.startActivity(this, MyICCardActivity.class);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == NEED_RELOAD && data != null) {
                boolean need_reload_flag = data.getBooleanExtra("need_reload_flag", false);
                if (need_reload_flag) {
                    loadData();
                }
            }

        }

    }


}
